package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    //首页数据概览
    @Override
    public OverviewVO getOverview() {
        //1.查询redis缓存中的数据
        log.info("从Redis中，查询线索和商机的概览数据：");
        Object object = redisTemplate.opsForValue().get("OVERVIEW_DATA");

        if (object != null) {//缓存中有数据
            log.info("从Redis中，查询线索和商机的概览数据成功：");
            return (OverviewVO) object;
        }
        //2.缓存中没有数据，则查询数据库
        log.info("首页数据概览：");
        //2.1获取线索对应的数据
        OverviewVO clueOverview = clueMapper.getClueOverview();
        //2.2获取商机对应的数据
        OverviewVO businessOverview = businessMapper.getBusinessOverview();
        //2.3封装数据并返回
        BeanUtils.copyProperties(clueOverview,businessOverview,"businessTotal","businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");

        //3.将数据保存到Redis中
        log.info("将数据保存到Redis中：");
        redisTemplate.opsForValue().set("OVERVIEW_DATA",businessOverview,1, TimeUnit.MINUTES);

        return businessOverview;
    }
}
